"""
图片生成模型
"""

from sqlalchemy import Column, Integer, String, DateTime, Text, ForeignKey, JSON
from sqlalchemy.sql import func
from sqlalchemy.orm import relationship
from app.database import Base


class ImageGeneration(Base):
    """图片生成记录表"""
    
    __tablename__ = "image_generations"
    
    id = Column(Integer, primary_key=True, index=True, comment="图片生成ID")
    conversation_id = Column(Integer, ForeignKey("conversations.id"), nullable=False, comment="对话ID")
    user_id = Column(Integer, ForeignKey("users.id"), nullable=False, comment="用户ID")
    prompt = Column(Text, nullable=False, comment="图片生成提示词")
    original_image_id = Column(Integer, ForeignKey("image_generations.id"), nullable=True, comment="原始图片ID(用于调整)")
    image_url = Column(String(500), nullable=False, comment="生成的图片URL")
    image_path = Column(String(500), comment="图片存储路径")
    model_provider = Column(String(50), comment="模型提供商(如OpenAI, Stability AI)")
    model_name = Column(String(100), comment="使用的模型名称")
    size = Column(String(20), comment="图片尺寸")
    quality = Column(String(20), comment="图片质量")
    style = Column(String(50), comment="图片风格")
    image_metadata = Column(JSON, comment="额外元数据")
    created_at = Column(DateTime(timezone=True), server_default=func.now(), comment="创建时间")
    
    # 关系
    user = relationship("User")
    conversation = relationship("Conversation")
    # 自引用关系 - 用于调整后的图片
    adjustments = relationship("ImageGeneration", backref="original_image", remote_side=[id])
    
    def __repr__(self):
        return f"<ImageGeneration(id={self.id}, user_id={self.user_id}, prompt='{self.prompt[:30]}...')>"

